﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class campaignEmailDomainPerformanceOutput : Api_BaseOutput
   {
      public List<campaignEmailDomainPerformanceResults> result { get; set; }
      public campaignEmailDomainPerformanceParms inputParms { get; private set; }
      //
      public campaignEmailDomainPerformanceOutput( campaignEmailDomainPerformanceInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new List<campaignEmailDomainPerformanceResults>();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "apikey added: {0}", this.result );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_campaignEmailDomainPerformance[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            IEnumerable<XElement> entries = xml.Elements( "struct" );
            foreach ( XElement entry in entries )
            {
               var stat = new campaignEmailDomainPerformanceResults();

               stat.domain = entry.Element( "domain" ).Value;
               stat.total_sent = int.Parse( entry.Element( "total_sent" ).Value );
               stat.emails = int.Parse( entry.Element( "emails" ).Value );
               stat.bounces = int.Parse( entry.Element( "bounces" ).Value );
               stat.opens = int.Parse( entry.Element( "opens" ).Value );
               stat.clicks = int.Parse( entry.Element( "clicks" ).Value );
               stat.unsubs = int.Parse( entry.Element( "unsubs" ).Value );
               stat.delivered = int.Parse( entry.Element( "delivered" ).Value );
               stat.emails_pct = float.Parse( entry.Element( "emails_pct" ).Value );
               stat.bounces_pct = float.Parse( entry.Element( "bounces_pct" ).Value );
               stat.opens_pct = float.Parse( entry.Element( "opens_pct" ).Value );
               stat.clicks_pct = float.Parse( entry.Element( "clicks_pct" ).Value );
               stat.unsubs_pct = float.Parse( entry.Element( "unsubs" ).Value );

               this.result.Add( stat );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();
         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               this.result = jss.Deserialize<List<campaignEmailDomainPerformanceResults>>( this.api_Response );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- Format Results
      protected void FormatResults( x_campaignEmailDomainPerformance[] rpcResult )
      {
         foreach ( var result in rpcResult )
         {
            var stat = new campaignEmailDomainPerformanceResults();
            //
            stat.domain = result.domain;
            stat.total_sent = result.total_sent;
            stat.emails = result.emails;
            stat.bounces = result.bounces;
            stat.opens = result.opens;
            stat.clicks = result.clicks;
            stat.unsubs = result.unsubs;
            stat.delivered = result.delivered;
            // from int to float
            stat.emails_pct = result.emails_pct;
            stat.bounces_pct = result.bounces_pct;
            stat.opens_pct = result.opens_pct;
            stat.clicks_pct = result.clicks_pct;
            stat.unsubs_pct = result.unsubs;
            //
            this.result.Add( stat );
         }
      }

      #endregion
   }
}
